/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.SerialNumberDao;
import com.minierp.model.SerialNumber;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class SerialNumberDaoImpl extends HibernateDaoSupport
		implements
			SerialNumberDao {
	public int getCurrentNum(long ruleId, String prefix) {
		Class arg3 = SerialNumberDaoImpl.class;
		synchronized (SerialNumberDaoImpl.class) {
			HibernateTemplate ht = this.getHibernateTemplate();
			String sql = "from com.minierp.model.SerialNumber s where s.ruleId="
					+ ruleId + " and s.prefix=:prefix";
			String[] paramNames = new String[]{"prefix"};
			Object[] values = new Object[]{prefix};
			List list = ht.findByNamedParam(sql, paramNames, values);
			SerialNumber obj = null;
			if (!list.isEmpty()) {
				obj = (SerialNumber) list.get(0);
			}

			int retVal;
			if (obj == null) {
				retVal = 1;
				obj = new SerialNumber();
				obj.setCurrentNum(retVal);
				obj.setPrefix(prefix);
				obj.setRuleId(ruleId);
				ht.save(obj);
			} else {
				retVal = obj.getCurrentNum() + 1;
				obj.setCurrentNum(retVal);
				ht.update(obj);
			}

			return retVal;
		}
	}
}